// pages/selectRole/selectRole.js
var userModel = require('../../models/user.js')
var role = null;
let klassSid, klass = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioItems: [
      { name: '我是班主任', value: '1' },
      { name: '我是任课老师', value: '0' },
      { name: '我是家长', value: '100' }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    klassSid = options.klassSid;
    if (getApp().globalData.param.klass) {
      klass = getApp().globalData.param.klass
    }

    console.log(klass)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    klassSid = null
    klass = null
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    role = null
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  radioChange: function (e) {
    role = e.detail.value;
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems
    });
  },

  onNext:function(event) {
    
    if (null == role) {
      wx.showModal({
        title: '提示',
        content: '请确认身份',
        showCancel:false
      })

      return;
    }

    getApp().createFormId(event.detail.formId)
    
    if (role == userModel.PATRIARCH) {
      if (klass && '0' == klass.studentCount) {
        wx.navigateTo({
          url: '../editStudent/editStudent?klassSid=' + klassSid,
        })
      }else {
        wx.navigateTo({
          url: '../studentList/studentList?klassSid=' + klassSid,
        })
      }     
    } else if (role == userModel.HEAD_TEACHER || role == userModel.TEACHER){
      wx.navigateTo({
        url: '../editTeacher/editTeacher?role=' + role + '&klassSid=' + klassSid,
      })
    }
  }
})